package com.travel.server.manager.impl.command.message;

import com.travel.common.CommonConstant;
import com.travel.server.manager.dto.request.message.UpdateMessageMailRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.MessageDal;
import com.travel.server.manager.impl.enums.MessageTemplateEnum;
import com.travel.server.manager.model.MessageTemplate;
import com.travel.server.manager.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: LiZhenhui
 * @Date: 2018\11\15 0015 22:22
 * @Desc:
 */
@Component
@Slf4j
public class UpdateMessageMailCommand extends AbstractCommand<UpdateMessageMailRequest, EmptyResponse> {

    @Autowired
    private MessageDal messageDal;

    @Override
    protected void checkInput(UpdateMessageMailRequest request) {
    }

    @Override
    public EmptyResponse handle(UpdateMessageMailRequest request) {
        UserInfo userInfo = getUserInfo();
        Date now = new Date();

        Integer id = request.getId();
        MessageTemplate messageTemplate;
        if(id == null){
            messageTemplate = new MessageTemplate();
            messageTemplate.setState(CommonConstant.YES);
            messageTemplate.setCreateBy(userInfo.getName());
            messageTemplate.setCreateTime(now);
            messageTemplate.setMessageType(MessageTemplateEnum.MAIL.getType());
        }else{
            messageTemplate = messageDal.selectById(id);
        }
        messageTemplate.setMailSender(request.getSender());
        messageTemplate.setMailTheme(request.getTheme());
        messageTemplate.setMailRemark(request.getRemark());
        messageTemplate.setMessageContent(request.getContent());
        messageTemplate.setMessageTitle(request.getName());
        messageTemplate.setUpdateBy(userInfo.getName());
        messageTemplate.setUpdateTime(now);

        messageDal.inserOrUpdate(messageTemplate);

        return new EmptyResponse();
    }
}
